#include "ScoreValue.h"
#include "Transformation.h"
#include "SimpleText.h"
#include "NetworkManager.h"
#include "Helpers.hpp"

using namespace game_engine;
using namespace game_engine::library;
using namespace game_engine::components;
using namespace game_engine::open_gl;
using namespace game_engine::utils;
using namespace RakNet;

///////////////////////////////////////////////////////////////////////////////
// Constructors and Destructors
///////////////////////////////////////////////////////////////////////////////

ScoreValue::ScoreValue(void)
    : GameObject()
{
}

ScoreValue::ScoreValue(Scene *scn, const string &n, const string &t, 
                       const DrawLevelType d, RPC3 *rpcFromNetwork)
    : GameObject(scn, n, t, d)
{
    // Configure network properties
    NetworkManager *networkManager = NetworkManager::getInstance();
    SetNetworkIDManager(&(networkManager->getNetworkIDManager()));
    if (rpcFromNetwork == 0)
    {
        SetNetworkID(PLAYER_SCORE_ID);
    }
    else
    {
        SetNetworkID(ENEMY_SCORE_ID);
    }
    RPC3_REGISTER_FUNCTION(networkManager->getRpc3Inst(), &ScoreValue::incrementScore);

    // Create its components
    Transformation *svTransf = new Transformation(this, 0.0, 0.0, 0.0, 0.0, 
        1.0, 1.0);
    SimpleText *svText = new SimpleText(this, HELVETICA_12, 
        ColorRGB(255.0, 255.0, 255.0));

    // Add components to object
    addComponent(svTransf);
    addComponent(svText);
}

ScoreValue::~ScoreValue(void)
{
}

///////////////////////////////////////////////////////////////////////////////
// Other public methods
///////////////////////////////////////////////////////////////////////////////

void ScoreValue::incrementScore(const UINT32 offset, RPC3 *rpcFromNetwork)
{
    try
    {
        // Get the SimpleText component
        SimpleText *txt = dynamic_cast<SimpleText*> (getComponent("SimpleText"));
        INT32 val = atoi((txt->getText()).c_str());
        val += offset;
        txt->setText(to_string<INT32>(val, dec));

        if (rpcFromNetwork == 0)
        {
            // Update score on the other machines
            NetworkManager *networkManager = NetworkManager::getInstance();
            (networkManager->getRpc3Inst())->CallCPP("&ScoreValue::incrementScore", 
                ENEMY_SCORE_ID, offset, rpcFromNetwork);
        }
    } catch (...)
    {
        // Nothing to do here
        return;
    }
}

///////////////////////////////////////////////////////////////////////////////
// Setters
///////////////////////////////////////////////////////////////////////////////

void ScoreValue::setPosition(const Point &pos) throw(...)
{
    // Get the Transformation component
    Transformation *transf = 
        dynamic_cast<Transformation*> (getComponent("Transformation"));
    transf->setPosition(pos);
}

void ScoreValue::setScore(const UINT32 val) throw(...)
{
    // Get the SimpleText component
    SimpleText *txt = dynamic_cast<SimpleText*> (getComponent("SimpleText"));
    txt->setText(to_string<UINT32>(val, dec));
}
